﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace SpaceWolf.GameLogic
{
    public class Laserbolt
        {
            public static int iBolts = 0;
            private int whichbolt = 0;
            private Random random_number = new Random();
            public bolt_manager[] bolt = new bolt_manager[20];// this will be an array of bolts
            Matrix[] shader_matrices_combined = new Matrix[2];

            public void setup_and_fire(Vector3 fstart, Vector3 fDirection, Microsoft.Xna.Framework.Graphics.Color fcolor, float speed, bool randoms)
            {
                bolt[whichbolt].start_position = fstart;

                bolt[whichbolt].bolt_speed = speed;
                bolt[whichbolt].color = fcolor;
                bolt[whichbolt].alive = true;
                bolt[whichbolt].first_frame = true;
                Laserbolt.iBolts++;
                if (randoms) // adds some random noise to the direction
                {
                    bolt[whichbolt].direction.X = fDirection.X + (float)(random_number.Next(100) - 50) / 1000f;
                    bolt[whichbolt].direction.Y = fDirection.Y + (float)(random_number.Next(100) - 50) / 1000f; 
                    bolt[whichbolt].direction.Z = fDirection.Z + (float)(random_number.Next(100) - 50) / 1000f;

                }
                else
                {
                    bolt[whichbolt].direction = fDirection;    
                }
                //bolt[whichbolt].direction.Z = fDirection.Z +50;
                
                bolt[whichbolt].world_matrix = Matrix.Identity* Matrix.CreateTranslation(fstart);

                bolt[whichbolt].direction.Normalize();
                
                whichbolt++;
                if (whichbolt == 20) whichbolt = 0;  // only allows 20 laserbolts at a time, usually that is plenty.
            }

            public void Update(float distance)
            {
                for (int iCount = 0; iCount < 20; iCount++)
                {
                    bolt[iCount].update(distance);
                }
            }

            public void Initialize()
            {
                for (int i = 0; i < 20; i++)
                {
                    bolt[i] = new bolt_manager();
                    bolt[i].world_matrix = Matrix.Identity;
                }
            }

            public class bolt_manager
            {
                public Matrix world_matrix = Matrix.Identity;
                public Vector3 center_to_viewer,
                               work_vector,
                               direction,
                               trans_save;

                public bool alive = false;
                public BoundingSphere boundSphere;
                public float bolt_speed = 0;
                public float distance_travelled;
                public bool first_frame = false;

                public Vector3 start_position;

                public Microsoft.Xna.Framework.Graphics.Color color = Microsoft.Xna.Framework.Graphics.Color.CornflowerBlue;

                public void update(float distance)
                {
                    if (alive)
                    {
                        #region move the world matrix

                        distance_travelled = Vector3.Distance(start_position, world_matrix.Translation);

                        if (distance_travelled < distance) // set a maximum allowable distance here
                        {
                            world_matrix.Translation += direction * bolt_speed;

                        }
                        else
                        {
                            Laserbolt.iBolts--;
                            alive = false;
                        }


                        // you will need to setup some collision detection somewhere to kill the bolt on impact

                        if (!first_frame)
                        {
                            world_matrix.Translation += direction * bolt_speed;
                        }

                        work_vector = world_matrix.Translation;

                        work_vector.Normalize();
                        // - work_vector is camera to laser direction

                        if (first_frame == true)
                            first_frame = false;

                        // work vector will be used to calculate the orientation of the laserbolt.
                        boundSphere.Center = world_matrix.Translation;
                        boundSphere.Radius = 1f;
                        #endregion

                        #region build the world matrix

                        world_matrix.Forward = direction;

                        world_matrix.Up = Vector3.Normalize(Vector3.Cross(world_matrix.Forward, -work_vector));

                        world_matrix.Right = Vector3.Normalize(Vector3.Cross(world_matrix.Forward, world_matrix.Up));
                        center_to_viewer = world_matrix.Up; // will find right & left in the shader

                        #endregion
                    }
                    else
                    {
                        world_matrix = Matrix.Identity;
                        
                    }
                }


            }//end bolt manager
        }

        //Laserbolt Laserbolts = new Laserbolt();
}
